package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Classinfo;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.qo.ClassinfoQuery;
import cn.wolfcode.crm.service.IClassinfoService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"classinfo:list","班级页面"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo")ClassinfoQuery qo){
        //班级数据
        PageInfo pageInfo = classinfoService.query(qo);
        model.addAttribute("result",pageInfo);
        //班主任
        List<Employee> employees = employeeService.selectByclassAll();
        model.addAttribute("emps",employees);
        List<Employee> all = employeeService.selectAll();
        model.addAttribute("empsAll",all);
        return "/classinfo/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"classinfo:saveOrUpdate","班级新增/编辑"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Classinfo classinfo){
        if (classinfo.getId() == null) {
            classinfoService.save(classinfo);
        }else{
            classinfoService.updateByPrimaryKey(classinfo);
        }
        return new JsonResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"classinfo:delete","班级删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        classinfoService.deleteByPrimaryKey(id);
        return new JsonResult();
    }
}
